/*
 * @lc app=leetcode.cn id=25 lang=golang
 *
 * [25] K 个一组翻转链表
 */

// @lc code=current
/**
* Definition for singly-linked list.
* type ListNode struct {
*     Val int
*     Next *ListNode
* }
1->2->3->4->5
*/

func reverseKGroup(head *ListNode, k int) *ListNode {
	node := head
	for i := 0; i < k; k++ {
		if node == nil {
			return head
		}
		node = node.Next
	}

	newhead := reverse(head, node)
	head.Next = reverseKGroup(node, k)

	return newhead
}

func reverse(head, last *ListNode) *ListNode {
	prev := last
	current := head
	for current != last {
		tmp := current.Next
		current.Next = prev
		prev = current
		current = tmp
	}
}

// @lc code=end

